package LinkedList;

import java.math.BigDecimal;

public class _24_AddTwoNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        String num1 = "";
        String num2 = "";
        while (l1 != null) {
            num1+=l1.val;
            l1 = l1.next;
        }
        while (l2 != null) {
            num2+=l2.val;
            l2 = l2.next;
        }
        StringBuffer sb1 = new StringBuffer(num1);
        StringBuffer sb2 = new StringBuffer(num2);
        num1 = sb1.reverse().toString();
        num2 = sb2.reverse().toString();
        BigDecimal bd = new BigDecimal(num1);
        BigDecimal bd2 =new BigDecimal(num2);
        bd = bd.add(bd2);
        String sum = new StringBuffer(bd.toString()).reverse().toString();
        ListNode head =new ListNode(0);
        ListNode p = head;
        int length = sum.length();
        for(int i=0;i<length;i++) {
            p.next = new ListNode(Integer.parseInt(sum.charAt(i)+""));
            p = p.next;
        }
        return head.next;
    }
}
